package com.springmvc.core.db;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource {

	static final Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);

	/**
	 * 取得当前使用哪个数据源
	 * 获取完后解除绑定，防止其他的方法调用该数据源
	 */
	@Override
	protected Object determineCurrentLookupKey() {
		Object dataSource = DbContextHolder.getDbType();
		logger.debug("获取数据源dataSource：{}...", dataSource);
		DbContextHolder.clearDbType();
		return dataSource;
	}
}
